import json
from datetime import datetime, timedelta

import cv2
import numpy as np
from flask import Blueprint, render_template, request, current_app

from applications.common.curd import model_to_dicts
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, success_api, fail_api
from applications.common.utils.validate import xss_escape
from applications.extensions import db
from applications.models import CourseStandard, ActionInfo, KeyframeInfo, StandardResult, PoliceModels
from applications.schemas import StandardSchema, ActionSchema, KeyframeInfoSchema, PoliceOutSchema, StandardResultSchema
from applications.common.course.generate import CourseGenerator
from sqlalchemy import desc

course_standard_bp = Blueprint('courseStandard', __name__, url_prefix='/course/standard')

# 获取成绩页面
@course_standard_bp.get('/result')
def getResultPage():
    return render_template('admin/course/action_standard/result.html')


# 获取成绩信息
@course_standard_bp.get('/result/data')
def get_result_data():
    # 获取请求参数
    code = request.args.get('code', type=str)
    name = request.args.get('police_name', type=str)
    sex = request.args.get('sex', type=int)
    birthday = request.args.get('birthday', type=str)
    id_number = request.args.get('id_number', type=str)
    native_place = request.args.get('native_place', type=str)
    address = request.args.get('address', type=str)
    post = request.args.get('post', type=str)
    title = request.args.get('title', type=str)
    dept_name = request.args.get('dept_name', type=str)

    # 查询参数构造
    mf = ModelFilter()
    if code:
        mf.exact(field_name="code", value=code)
    if name:
        mf.contains(field_name="name", value=name)
    if sex:
        mf.contains(field_name="sex", value=sex)
    if birthday:
        mf.contains(field_name="birthday", value=birthday)
    if id_number:
        mf.contains(field_name="id_number", value=id_number)
    if native_place:
        mf.contains(field_name="native_place", value=native_place)
    if address:
        mf.contains(field_name="address", value=address)
    if post:
        mf.contains(field_name="post", value=post)
    if title:
        mf.contains(field_name="title", value=title)
    if dept_name:
        mf.contains(field_name="dept_name", value=dept_name)
    # orm查询
    # 使用分页获取data需要.items
    police = PoliceModels.query.filter(mf.get_filter(model=PoliceModels)).layui_paginate()
    count = police.total
    return table_api(data=model_to_dicts(schema=PoliceOutSchema, data=police.items), count=count)


@course_standard_bp.get('/result/detail/<int:student_id>')
def result_details(student_id):
    # 获取请求参数
    action_Name = xss_escape(request.args.get('action_Name', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if action_Name:
        mf.contains(field_name="action_Name", value=action_Name)

    results = StandardResult.query.order_by('train_time').filter_by(student_id=student_id).layui_paginate()
    results_dict = model_to_dicts(schema=StandardResultSchema, data=results.items)
    for result in results_dict:
        action = ActionInfo.query.filter_by(action_id=result['action_id']).with_entities(ActionInfo.course_id,
                                                                                         ActionInfo.action_id,
                                                                                         ActionInfo.action_name,
                                                                                         ActionInfo.action_description,
                                                                                         ActionInfo.action_path,
                                                                                         ActionInfo.action_length,
                                                                                         ActionInfo.action_loadtime,
                                                                                         ActionInfo.action_updatetime,
                                                                                         ActionInfo.icon_path).one_or_none()
        if action is not None:
            result['action_name'] = action.action_name
    count = results.total
    return render_template('admin/course/action_standard/result_detail.html', data=results_dict, count=count)
